package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 作用：根据手机号查找用户
     *
     * @param mobile 手机号
     * @return
     */
    @GetMapping("/findByMobile")
    public ResponseEntity<Object> findByMobile(String mobile) {
        int i = 1 / 0;
        return userService.findByMobile(mobile);
    }


    /**
     * 作用： 保存用户
     *
     * @param user
     * @return
     */
    @PostMapping
    public ResponseEntity<Object> save(@RequestBody User user) {//如果是json字符串过来，那么必须是post请求
        return userService.save(user);
    }


    /**
     * 接口名称：登录第一步---手机号登录
     * 接口路径：POST/user/login
     */
    @PostMapping("/login")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> param) {  // {phone:13556082481}
        //1. 取出手机号
        String phone = param.get("phone");
        return userService.sendSms(phone);
    }

    /**
     * 接口名称：登录第二步---验证码校验
     * 接口路径：POST/user/loginVerification
     */
    @PostMapping("/loginVerification")
    public ResponseEntity<Object> loginVerification(@RequestBody Map<String, String> param) {
        //1. 取出手机号
        String phone = param.get("phone");
        String verificationCode = param.get("verificationCode");
        return userService.loginVerification(phone, verificationCode);
    }


    /**
     * 接口名称：新用户---1填写资料
     * 接口路径：POST/user/loginReginfo
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity<Object> loginReginfo(@RequestBody UserInfo userInfo,
                                               @RequestHeader("Authorization") String token) {
        return userService.saveUserInfo(userInfo, token);
    }


    /**
     * 接口名称：新用户---2选取头像
     * 接口路径：POST /user/loginReginfo/head
     * 注意： 对于上传文件的类型必须是MultipartFile类型
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity<Object> updateHeader(MultipartFile headPhoto,
                                               @RequestHeader("Authorization") String token) throws IOException {

        return userService.updateHeader(headPhoto, token);
    }
}

